﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace WCFMINERP.ComControls
{

	public static class ConvertEx {
		public static bool IsZipFile(this string filePath) {
			if (string.IsNullOrEmpty(filePath)) {
				return false;
			}
			//			PathObject pathObject = new PathObject(filePath);
			//			if (pathObject.IsFile) {
			string ext = Path.GetExtension(filePath);
			//				FileInfo file = new FileInfo(filePath);
			if (ext.ToUpper() == ".RAR" || ext.ToUpper() == ".ZIP" || ext.ToUpper() == ".7Z" || ext.ToUpper() == ".KZ") {
				return true;
			}
			//			}
			return false;
		}
		public static bool IsImage(this string filePath) {
			if (string.IsNullOrEmpty(filePath)) {
				return false;
			}
			//			PathObject pathObject = new PathObject(filePath);
			//			if (pathObject.IsFile) {
			string ext = Path.GetExtension(filePath);
			//				FileInfo file = new FileInfo(filePath);
			if (ext.ToUpper() == ".JPG" || ext.ToUpper() == ".JPEG" || ext.ToUpper() == ".TIF" || ext.ToUpper() == ".TIFF" ) {
				return true;
			}
			//			}
			return false;
		}
		public static float CM2Inch(this float cm) {
			return (float) (cm/2.54);
		}
		
		
		public static T ConvertTo<T>(this IConvertible convertibleValue) {
			if (null == convertibleValue) {
				return default(T);
			}
			if (!typeof (T).IsGenericType) {
				return (T) Convert.ChangeType(convertibleValue, typeof (T));
			}
			Type genericTypeDefinition = typeof (T).GetGenericTypeDefinition();
			if (genericTypeDefinition == typeof (Nullable<>)) {
				return (T) Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(typeof (T)));
			}
			throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", convertibleValue.GetType().FullName,
														typeof (T).FullName));
		}
		/// <summary>
		///     填入字符串返回一个将中文字符替换成改汉字的拼音首字母的字符串
		/// </summary>
		/// <param name="txt"></param>
		/// <returns></returns>
        //public static string GetFirstSpell(string txt) {
        //    if (string.IsNullOrWhiteSpace(txt)) {
        //        return string.Empty;
        //    }
        //    StringBuilder rval = new StringBuilder();
        //    foreach(char c in txt) {
        //        if (ChineseChar.IsValidChar(c)) {
        //            ChineseChar chineseChar = new ChineseChar(c);
        //            ReadOnlyCollection<string> ddd = chineseChar.Pinyins;
        //            if (ddd != null && ddd.Count > 0) {
        //                rval.Append(ddd[0][0]);
        //            }
        //        }
        //        else {
        //            rval.Append(c);
        //        }
        //    }
        //    return rval.ToString().ToUpper();
        //}
		public static bool ComparerDouble(double d1, double d2) {
			double d3 = 0.00001;
			if (Math.Abs(d1 - d2) < d3) {
				return true;
			}
			else {
				return false;
			}
		}
	}

	public class SizeEx : ICloneable {
		public SizeEx() {
			XResolution = 1;
			YResolution = 1;
		}

		#region Overrides of Object

		#region Overrides of Object

		/// <summary>
		///     确定指定的 <see cref="T:System.Object" /> 是否等于当前的 <see cref="T:System.Object" />。
		/// </summary>
		/// <returns>
		///     如果指定的 <see cref="T:System.Object" /> 等于当前的 <see cref="T:System.Object" />，则为 true；否则为 false。
		/// </returns>
		/// <param name="obj">与当前的 <see cref="T:System.Object" /> 进行比较的 <see cref="T:System.Object" />。</param>
		public override bool Equals(object obj) {
			SizeEx sizeEx = obj as SizeEx;
			return sizeEx != null && sizeEx.PixWidth == this.PixWidth && sizeEx.PixHeight == this.PixHeight;
		}

		#endregion

		/// <summary>
		///     返回表示当前 <see cref="T:System.Object" /> 的 <see cref="T:System.String" />。
		/// </summary>
		/// <returns>
		///     <see cref="T:System.String" />，表示当前的 <see cref="T:System.Object" />。
		/// </returns>
		public override string ToString() {
			return SizeCMDesc;
		}

		#endregion

		public string SizeCMDesc {
			get { return String.Format("{0:F2}X{1:F2} cm", CMSize.Width, CMSize.Height); }
		}
		public string SizeCMDesc2 {
			get { return String.Format("{0:0.##}X{1:0.##}", CMSize.Width, CMSize.Height); }
		}
		public string SizePixDesc {
			get { return String.Format("{0}X{1} pix", PixWidth, PixHeight); }
		}
		public string SizeInchDesc {
			get { return String.Format("{0:F2}X{1:F2} inch", InchSize.Width, InchSize.Height); }
		}
		public string SizeInchDesc2 {
			get { return String.Format("{0:0.##}X{1:0.##}", InchSize.Width, InchSize.Height); }
		}
		public string SizeCMTDesc {
			get { return String.Format("{0:F2}X{1:F2}X{2:F2} cm", CMSize.Width, CMSize.Height, CMSize.Long); }
		}
		public string SizePixTDesc {
			get { return String.Format("{0}X{1}X{2} pix", PixWidth, PixHeight, PixLong); }
		}
		public string SizeInchTDesc {
			get { return String.Format("{0:F2}X{1:F2}X{2:F2} inch", InchSize.Width, InchSize.Height, InchSize.Long); }
		}
		public SizeEx(float inchWidth, float inchHeight, float inchLong, int xResolution, int yResolution) {
			fPixWidth = (int) (inchWidth*xResolution);
			fPixHeight = (int) (inchHeight*yResolution);
			fPixLong = (int) (inchLong*xResolution);
			XResolution = xResolution;
			YResolution = yResolution;
			InchSize = new SizeTF(inchWidth, inchHeight, inchLong);
			CMSize = GetCM();
		}
		public int SizeId { get; set; }
		public SizeEx(float inchWidth, float inchHeight, int xResolution, int yResolution) {
			fPixWidth = (int) (inchWidth*xResolution);
			fPixHeight = (int) (inchHeight*yResolution);
			XResolution = xResolution;
			YResolution = yResolution;
			InchSize = new SizeTF(inchWidth, inchHeight);
			CMSize = GetCM();
		}
		
		public SizeEx(int width, int height, int xResolution, int yResolution) {
			fPixHeight = height;
			fPixWidth = width;
			XResolution = xResolution;
			YResolution = yResolution;
			InchSize = GetInch();
			CMSize = GetCM();
		}
		public SizeTF CMSize { get; private set; }
		public SizeTF InchSize { get; private set; }
		public int PixHeight {
			get { return (int) fPixHeight; }
		}
		public int PixLong {
			get { return (int) fPixLong; }
		}
		public int PixWidth {
			get { return (int) fPixWidth; }
		}
		public double fPixHeight { get; private set; }
		public double fPixWidth { get; private set; }
		public double fPixLong { get; private set; }
		public int XResolution { get; private set; }
		public int YResolution { get; private set; }
		public void SetCM(float cmWidth, float cmHeight, float cmLong = 0) {
			fPixWidth = (float) (cmWidth*XResolution/2.54);
			fPixHeight = (float) (cmHeight*YResolution/2.54);
			fPixLong = (float) (cmLong*YResolution/2.54);
			InchSize = GetInch();
			CMSize = GetCM();
		}
		public void SetInch(float inchWidth, float inchHeight, float inchLong = 0) {
			fPixWidth = inchWidth*XResolution;
			fPixHeight = inchHeight*YResolution;
			fPixLong = inchLong*XResolution;
			//InchSize = GetInch();
			InchSize = new SizeTF(inchWidth, inchHeight, inchLong);
			CMSize = GetCM();
		}
		private SizeTF GetInch() {
			SizeTF sizeF = new SizeTF();
			sizeF.Width = (float) (fPixWidth/XResolution);
			sizeF.Height = (float) (fPixHeight/YResolution);
			sizeF.Long = (float) (fPixLong/XResolution);
			return sizeF;
		}
		private SizeTF GetCM() {
			SizeTF sizeTf = InchSize;
			SizeTF sizeF = new SizeTF();
			sizeF.Width = (float) (sizeTf.Width*2.54); // (float) (fPixWidth/XResolution*2.54);
			sizeF.Height = (float) (sizeTf.Height*2.54); // (float)(fPixHeight / YResolution * 2.54);
			sizeF.Long = (float) (sizeTf.Long*2.54); //(float)(fPixLong / XResolution * 2.54);
			return sizeF;
		}

		#region Implementation of ICloneable

		/// <summary>
		///     创建作为当前实例副本的新对象。
		/// </summary>
		/// <returns>
		///     作为此实例副本的新对象。
		/// </returns>
		public object Clone() {
			SizeEx sizeEx = new SizeEx();
			sizeEx.fPixHeight = fPixHeight;
			sizeEx.fPixWidth = fPixWidth;
			sizeEx.fPixLong = fPixLong;
			sizeEx.XResolution = XResolution;
			sizeEx.YResolution = YResolution;
			sizeEx.InchSize = sizeEx.GetInch();
			sizeEx.CMSize = sizeEx.GetCM();
			return sizeEx;
		}

		#endregion
	}

	public struct SizeTF {
		public SizeTF(float width, float height, float lg = 0) : this() {
			Width = width;
			Height = height;
			Long = lg;
		}
		public float Long { get; set; }
		public float Width { get; set; }
		public float Height { get; set; }
		public bool IsEmpty {
			get { return Math.Abs(Width) < 0.00001 && Math.Abs(Height) < 0.00001 && Math.Abs(Long) < 0.00001; }
		}

		#region Overrides of ValueType

		/// <summary>
		///     指示此实例与指定对象是否相等。
		/// </summary>
		/// <returns>
		///     如果 <paramref name="obj" /> 和该实例具有相同的类型并表示相同的值，则为 true；否则为 false。
		/// </returns>
		/// <param name="obj">要比较的另一个对象。</param>
		public override bool Equals(object obj) {
			if (!(obj is SizeTF)) {
				return false;
			}
			SizeTF o = (SizeTF) obj;
			return Math.Abs(Width - o.Width) < 0.00001 && Math.Abs(Height - o.Height) < 0.00001 && Math.Abs(Long - o.Long) < 0.00001;
		}
		public static bool operator ==(SizeTF a, SizeTF b) {
			return a.Equals(b);
		}
		public static bool operator !=(SizeTF a, SizeTF b) {
			return a.Equals(b);
		}

		#endregion
	}
}
